package com.skh.array;

/**
 * @Author: skh
 * @Date: 2019/6/26 10:30
 * @Description: 485.最大连续1的个数
 */
public class FindMaxConsecutiveOnes {

    /*
    给定一个二进制数组， 计算其中最大连续1的个数。

    示例 1:

    输入: [1,1,0,1,1,1]
    输出: 3
    解释: 开头的两位和最后的三位都是连续1，所以最大连续1的个数是 3.
     */

    public int findMaxConsecutiveOnes(int[] nums) {
       int count = 0;
       int max = 0;

        for (int num : nums) {
            if (num == 1) {
                count++;
            }else {
                if (max < count) {
                    max = count;
                    count=0;
                }else {
                    count=0;
                }
            }
        }

        if (max < count) {
            max = count;
        }


        return max;
    }

    public static void main(String[] args) {
        FindMaxConsecutiveOnes findMaxConsecutiveOnes = new FindMaxConsecutiveOnes();
        int[] nums = {1,1,0,1,1,1};
        int maxConsecutiveOnes = findMaxConsecutiveOnes.findMaxConsecutiveOnes(nums);
        System.out.println("maxConsecutiveOnes = " + maxConsecutiveOnes);
    }
}
